<?
#####################################################
#  COPYRIGHT NOTICE
#####################################################
# In the following statements, the term "this 
# program" refers to all files associated with the
# execution and distribution of Adtrackz.
#
# This Program is Copyright 2004 Jonah Klimack.
# All Rights Reserved. 
#
# Selling the code for this program, modifying 
# and/or redistributing the code for this program
# in part or in whole over the Internet or in any 
# other medium is expressly forbidden. Violators
# will be prosecuted to the fullest extent of the law
# Copyright and header information may not be
# modified.
#
# This program is distributed "as is" and without 
# warranty of any kind, either express or implied.
# In no event shall the liability of Jonah Klimack 
# for any damages, losses and/or causes of 
# action exceed the total amount paid by the 
# user for this software.		
#####################################################
# DO NOT MODIFY ANYTHING IN THIS FILE
#####################################################

//need to tag the visitor with a cookie so that
//he doesn't see the same salesletter twice...
//make this an option...
//

require "config.php";

//handle env variables
if ($_SERVER['HTTP_REFERER'])
	$referer = $_SERVER['HTTP_REFERER'];
else
	$referer = "none";

//st.php?s=split_test_name
if (!$s)
{
	record_alert("no split test name specified in st.php"); //automatically exits
}
else
{
	$st_id = mysql_fetch_row(mysql_query("select ID from adtrackz_split_test_groups where name='$s'"));
	if (!$st_id[0])
		record_alert("split test name not found in st.php");

	//list of campaigns in this split test
	$campaigns = mysql_query("select campaignID from adtrackz_split_test_campaigns where split_test_ID='$st_id[0]'");

	
	$tag = mysql_fetch_row(mysql_query("select tagvisitor from adtrackz_split_test_groups where id='$st_id[0]'"));
	if ($tag[0])
	{
		//see if a campaign in this split test is in the visitor's cookie
		$vcampaigns_subs = explode("|", $_COOKIE['ccampaign']);
		foreach ($vcampaigns_subs as $item) 
		{
			list($vcampaign, $vsub) = explode("&",$item);
			while ($st_campaign = mysql_fetch_row($campaigns))
			{
				list($st_adcode,$st_url) = mysql_fetch_row(mysql_query("select adcode,url from adtrackz_campaigns where ID='$st_campaign[0]'"));
				if($st_adcode == $vcampaign)
				{
					//echo "* $st_adcode $st_url *";
					header("Location: http://$domain/$udirectory/go.php?c=$st_adcode");
					exit;
				}
			}
			mysql_data_seek($campaigns,0);
		}	
	}

	//got to here, means visitor has not been to this split test yet
	//or tag is not on
	//reset campaigns
	mysql_data_seek($campaigns,0);
	$num_campaigns = mysql_num_rows($campaigns);
	if (!$num_campaigns)
		record_alert("no campaigns found in $s in st.php");

	for ($i=0;$i<=$num_campaigns-1;$i++)
	{
		$cID = mysql_fetch_row($campaigns);
		$cIDs[$i] = $cID[0]; 
		//echo "$cID[0] <br>";
	}

	$random = rand(0,$num_campaigns-1);

	$campaign_name = mysql_fetch_row(mysql_query("select adcode from adtrackz_campaigns where ID='$cIDs[$random]'"));

	//echo "<br> $cIDs[$random] -> $campaign_name[0]";

	header("Location: http://$domain/$udirectory/go.php?c=$campaign_name[0]");

	exit();


}

function record_alert($reason)
{
	global $c,$s,$referer;
	mysql_query("insert into adtrackz_alerts values ('','-','-',now(),'$referer','$reason');");
	$default_url = mysql_fetch_row(mysql_query("select default_url from adtrackz_login"));
	//send visitor to default url
	//echo "default url";
	header("Location: $default_url[0]");
	exit;
}

?>